﻿using System.Collections.Generic;
using iControl;
using Microsoft.Web.Farm;

namespace F5LoadBalancer {
    [WebFarmOperationProvider]
    public class F5AddServerOperationProvider : WebFarmOperationProvider {
        public override string Name {
            get {
                return ProviderName;
            }
        }

        public override IEnumerable<OperationParameter> Parameters {
            get {
                yield return new OperationParameter(AddServerOperationProvider.ServerParameterName,
                    Resources.ServerParameterDescription,
                    typeof(ServerContext));
            }
        }

        public override object RunOperation(WebFarmOperationContext operationContext) {
            ServerContext serverContext = (ServerContext)operationContext.Options.Parameters[0].Value;
            Utility.WriteEntry("Adding server " + serverContext.Address + " to " + serverContext.WebFarm.Name + " farm");
            serverContext.TraceMessage (new TraceMessage (System.Diagnostics.TraceLevel.Info, "Adding server " + serverContext.Address + " to " + serverContext.WebFarm.Name + " farm"));
            Utility.CreatePoolMember(serverContext.WebFarm.Name, serverContext.Address, CommonEnabledState.STATE_DISABLED);
            
            return null;
        }

        public const string ProviderName = F5Constants.F5LoadBalancerName + AddServerOperationProvider.ProviderName;
    }
}
